package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.po.*;
import com.xueyi.aixihu.entity.vo.AxhFinanceVo;
import com.xueyi.aixihu.reqeust.DtNewsReq;
import com.xueyi.aixihu.reqeust.FinanceReq;
import com.xueyi.aixihu.reqeust.NewsTypeReq;
import com.xueyi.aixihu.reqeust.ApiSearchSystemTypeContentReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 描述 三务公开数据接口
 *
 * @author 刘梓江
 * @date 2023-07-01 09:40
 */
public interface ApiSanWuGongKaiMapper {




    /**
     * @return
     */
    @Select({
        "<script>",
                " select * from dt_news_sort where is_valid=1 and pid in  ",
                " ( ",
                        "<foreach collection='pid' item='item' index='index' separator=','>",
                            " #{item}" ,
                        "</foreach>",
                " ) ",
        "</script>",
    })
    List<DtNewsSort> selectVillageCommunity(@Param("pid") List<Integer> pid);

    @Select({
            "<script>",
                " select * from dt_news_sort where id in  ",
                " ( ",
                    "<foreach collection='ids' item='item' index='index' separator=','>",
                     " #{item}" ,
                    "</foreach>",
                " ) ",
            "</script>",
    })
    List<DtNewsSort>  selectNewsSortById(@Param("ids") List<Integer> ids);

    @Select({
        "<script>",
           " select * from cdz_system_type where is_valid=1 and type=#{type} ",
        "</script>",
    })
    List<NewsTypeReq> selectSystemTypeByType(@Param("type") Integer type);

    
    @Select({
        "<script>",
            " select * from dt_news where is_valid=1 and is_public=1 and system_type_id=#{req.id}  ",
            " <if test='req.areaId!=null '> ",
            " and from_to_area_id =#{req.areaId} ",
            " </if> ",
            " <if test=' req.keyWord!=null and req.keyWord.trim()!=\"\" '> ",
            " and title like concat('%',#{req.keyWord},'%') ",
            " </if> ",
            " <if test=' req.year!=null and req.year.trim()!=\"\" '> ",
            " and DATE_FORMAT(create_time_new,'%Y')=#{req.year} ",
            " </if> ",
            " order by create_time_new desc  ",
            " limit #{req.startIndex},#{req.limit} ",
        "</script>",
    })
    List<DtNewsPo> selectDtNewsBySystemId(@Param("req") ApiSearchSystemTypeContentReq req);

    @Select({
        "<script>",
            " select * from dt_news where id=#{id} ",
        "</script>",
    })
    DtNewsPo selectDtNewsById(@Param("id") Integer id);

    @Select({
        "<script>",
            " select * from axh_finance where is_valid=1   and is_public=1  ",
            " <if test=' req.id!=null '> ",
            "  and system_type_id=#{req.id} ",
            " </if> ",
            " <if test=' req.id==null '> ",
            "  and report_type=#{req.searchType} ",
            " </if> ",
            " <if test=' req.keyWord!=null and req.keyWord.trim()!=\"\" '> ",
                " and finance_title like concat('%',#{req.keyWord},'%') ",
            " </if> ",
            " <if test=' req.areaId!=null '> ",
                " and from_to_area_id =#{req.areaId} ",
            " </if> ",
            " <if test=' req.year!=null and req.year.trim()!=\"\" '> ",
            " and DATE_FORMAT(create_time,'%Y')=#{req.year} ",
            " </if> ",
            " order by create_time desc ",
            " limit #{req.startIndex},#{req.limit} ",
        "</script>",
    })
    List<AxhFinanceVo> selectFinanceBySystemId(@Param("req") ApiSearchSystemTypeContentReq req);


    @Select({
        "<script>",
            " select * from axh_finance where id=#{id} ",
        "</script>",
    })
    AxhFinanceVo selectFinanceById(@Param("id") Integer id);

    @Select({
            "SELECT" +
            "   t1.* ",
            "   FROM ",
            "   cdz_audit_record t1 ",
            "   INNER JOIN ( SELECT MAX(id) AS id FROM cdz_audit_record where join_id in(${ids} ) and is_public=1 GROUP BY join_id ) t2 ON t1.id = t2.id"
    })
    List<CdzAuditRecord> selectNewsLastPublicRecord(@Param("ids") String ids);

    @Select({
            "SELECT" +
                    "   t1.* ",
            "   FROM ",
            "   axh_finance_log t1 ",
            "   INNER JOIN ( SELECT MAX(id) AS id FROM axh_finance_log where finance_id in(${ids}) and remark='发布' GROUP BY finance_id ) t2 ON t1.id = t2.id"
    })
    List<AxhFinanceLog> selectFinanceLastPublicRecord(@Param("ids") String ids);

}
